package com.xuecheng.system.controller;


import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.po.Company;
import com.xuecheng.system.service.CompanyService;
import com.xuecheng.web.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 教育机构 前端控制器
 * </p>
 *
 * @author itheima
 * @since 2024-04-30
 */
@Slf4j
@RestController
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    /*
    * 机构查询
    * */
    @GetMapping("/company/mine")
    public ResponseResult<CompanyVo> findCompany(@RequestHeader("authorization") String token){//1.接收请求头
//        //2.检验token合法性
//        Boolean verifyToken = JwtUtils.verifyToken(token);
//        if(verifyToken == false){
//            throw new BusinessException(ErrorCode.NOTTOKEN);
//        }
//        //3.解析token
//        AuthInfo authInfo = JwtUtils.getInfoFromToken(token);
//        if(authInfo.getCompanyId() == null){//如果不是企业用户,没有companyId
//            throw new BusinessException(ErrorCode.COMPANYERROR);
//        }
//        //4.根据企业id查询企业对象
//        Company company = companyService.getById(authInfo.getCompanyId());
//        //5.实体转vo
//        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
//        //6.返回结果
//        return ResponseResult.okResult(companyVo);

        //1.获取线程中的登录信息
        AuthInfo authInfo = AuthInfoHolder.getAuthInfo();
        if(authInfo.getCompanyId()==null){//如果不是企业用户,没有companeyId
            throw new BusinessException(ErrorCode.COMPANYERROR);
        }
        //2.根据企业id查询企业对象
        Company company = companyService.getById(authInfo.getCompanyId());
        //3.实体转vo
        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
        //4.返回结果
        return ResponseResult.okResult(companyVo);
    }

}
